// CategoryController.java
package com.question.controller.admin;

import com.question.pojo.dto.CategoryDTO;
import com.question.pojo.entity.Category;
import com.question.pojo.result.Result;
import com.question.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/manage/categories")
@Slf4j
@Api(tags = "分类管理相关接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 获取所有分类
     *
     * @return
     */
    @GetMapping
    @ApiOperation("获取所有分类")
    public Result<List<Category>> getAllCategories() {
        List<Category> categories = categoryService.getAllCategories();
        return Result.success(categories);
    }

    @PostMapping
    @ApiOperation("添加分类")
    public Result<Void> addCategory(@RequestBody CategoryDTO categoryDTO) {
        Category category = new Category();
        category.setMajorId(categoryDTO.getMajorId());
        category.setCategoryName(categoryDTO.getCategoryName());
        category.setDescription(categoryDTO.getDescription());
        categoryService.addCategory(category);
        return Result.success();
    }

    @PutMapping("/{categoryId}")
    @ApiOperation("更新分类")
    public Result<Void> updateCategory(@PathVariable Integer categoryId, @RequestBody CategoryDTO categoryDTO) {
        Category category = new Category();
        category.setCategoryId(categoryId);
        category.setMajorId(categoryDTO.getMajorId());
        category.setCategoryName(categoryDTO.getCategoryName());
        category.setDescription(categoryDTO.getDescription());
        categoryService.updateCategory(category);
        return Result.success();
    }

    @DeleteMapping("/{categoryId}")
    @ApiOperation("删除分类")
    public Result<Void> deleteCategory(@PathVariable Integer categoryId) {
        categoryService.deleteCategory(categoryId);
        return Result.success();
    }

    @GetMapping("/{categoryId}")
    @ApiOperation("根据ID获取分类")
    public Result<Category> getCategoryById(@PathVariable Integer categoryId) {
        Category category = categoryService.getCategoryById(categoryId);
        return Result.success(category);
    }

    @GetMapping("/by-major/{majorId}")
    @ApiOperation("根据专业ID获取分类")
    public Result<List<Category>> getCategoriesByMajorId(@PathVariable Integer majorId) {
        List<Category> categories = categoryService.getCategoriesByMajorId(majorId);
        return Result.success(categories);
    }
}